package com.duolaibei.business.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.api.ApiName;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.http.HttpUtils;
import com.duolaibei.business.model.ResponseResult;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.GsonUtil;
import com.duolaibei.business.utils.MD5;
import com.duolaibei.business.utils.QRCodeUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.utils.log.LogDebug;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by xlm on 2018/3/14 0014
 */

public class QrcodeCollectActivity extends BaseActivity {

    @Inject(R.id.back)
    private RelativeLayout mLayoutBack;
    @Inject(R.id.title)
    private TextView mViewTitle;
    @Inject(R.id.qrcode_collect_img)
    private ImageView mImageViewQrcode;
    @Inject(R.id.qrcode_save_photo)
    private LinearLayout mLayoutSave;
    private String appid;
    private String code;
    private String amount;
    private String appsecret;

    @Override
    protected void onGetIntent(Intent intent) {
        super.onGetIntent(intent);
        setIntent(intent);
        if (intent != null) {
            appid = intent.getStringExtra("appid");
            code = intent.getStringExtra("code");
            amount = intent.getStringExtra("amount");
            appsecret = intent.getStringExtra("appsecret");
        }
    }

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_qrcode_collect);
    }

    @Override
    protected void initViews() {
        mViewTitle.setText("收款码");
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        getQrcodeData();
//        getQrcode();
//        ftpTest();
//        ftpFuyouReq();
    }

    @Override
    protected void onSetListener() {
        mLayoutBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mLayoutSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveToPhoto();
            }
        });
    }

    //截图并保存相册
    private void saveToPhoto() {
        if (CommonUtil.getAndSaveCurrentImage(getThis(), String.valueOf(System.currentTimeMillis()))) {
            showToast("图片已保存到相册");
        }
    }

    /**
     * 获取支付二维码信息
     */
    private void getQrcodeData() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.PAYMENT_QRCODE);
        param.add("appid", appid);
        param.add("total_amount", amount);
        param.add("code", code);
        param.add("sign", param.getMd5String(appsecret));
        HttpUtils.getInstance().url(ApiName.PAYAPI).params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                if (CommonUtil.isForeground(getThis())) {
                    showToast(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (result.getStatus() == 1) {
                        String rqcode_url = null;
                        try {
                            rqcode_url = new JSONObject(string).getJSONObject("data").optString("rqcode_url");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (!CommonUtil.isEmpty(rqcode_url)) {
                            Log.i("TAG", "-----rqcode_url--------" + rqcode_url);
                            final Bitmap bitmap = QRCodeUtil.createQRImage(rqcode_url);
                            if (bitmap != null) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        mImageViewQrcode.setImageBitmap(bitmap);
                                    }
                                });
                            }
                        }
                    } else {
                        showToast(result.getInfo());
                    }
                } else {
                    showToast(string);
                }
            }
        });
    }
//
//    private void getQrcode() {
//
//        String data = "addtime=1536215553&agent_uid=40&appid=78d69f40906679a976dc4d45cebffbe6&body=%E5%A4%9A%E6%9D%A5%E8%B4%9D%E6%B5%8B%E8%AF%95&discountable_amount=0.00" +
//                "&external_order_no=981499972518661293727772233433&is_pay=1&merchant_name=%E9%A3%9E%E4%B9%90%E8%BF%AAKTV&notify_count=6" +
//                "&notify_url=http%3A%2F%2F%2Fduolaibei1.cailianpay.cn%2Fa%cn%2Fadmin.php%3Fs%3Fs%3D%2Fmerchants%2Ffuyou%2Fnotify" +
//                "&out_trade_no=CTI9061555342428D&pay_amount=0.01&pay_name=%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%8F%B7%E6%94%AF%E4%BB%98&pay_type=25" +
//                "&pay_uid=0&paytime=1536215710&product_id=0&service=cashier&shop_id=3021&status=1&success=0&terminal_id=813161&total_amount=0.01" +
//                "&trade_no=MYBKJHZF2018090614350123207101&uid=3616&uptime=1536215553&workmen_uid=3616&";
//        String md5sign = MD5.encode(data + "key=" + "jgxvrdISWpmbnlJFNMD0RUYZsEuc3KLO").toUpperCase();
//        LogDebug.d("------md5sign------" + md5sign);
//
//        StringBuilder stringBuilder = new StringBuilder();
//        for (int i = 0; i < 30; i++) {
//            int num = (int) (Math.random() * 9 + 1); //1~10之间的数字
//            stringBuilder.append(num);
//        }
//        String external_order_no = stringBuilder.toString();
////      LogDebug.d("------stringBuilder-------" + external_order_no);
//        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
//        param.add("appid", "e9510081ac30ffa83f10b68cde1cac07");
//        param.add("method", "payment.unifiedorder");
//        param.add("total_amount", "0.01");
//        param.add("code", "815908");
//        param.add("body", "多来贝测试");
//        try {
//            String urlencode = URLEncoder.encode("多来贝测试", "utf-8");
//            LogDebug.d("------urlencode------" + urlencode);
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        param.add("external_order_no", external_order_no);
//        param.add("notify_url", "http://duolaibei1.cailianpay.cn/admin.php?s=/merchants/fuyou/notify");
//        String sign = param.getMd5String("3YZlK24r57bFRoEL9IXGVMsHwSCzg0ak");
//        param.add("sign", sign);
//        LogDebug.d("------sign------" + sign);
//        HttpUtils.getInstance().url("http://www.duolaibei.com/Api/Gateway.html?").params(param).executePost(new HttpUtils.HttpListener() {
//            @Override
//            public void onHttpSuccess(String string) {
//                super.onHttpSuccess(string);
//                LogDebug.d("--------多来贝测试-------" + string);
//            }
//        });
//    }
//
//    private void ftpTest() {
//
//        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
//        param.add("hostname", "duolaibei1.cailianpay.cn");
//        param.add("port", "21");
//        param.add("username", "duolaibei1");
//        param.add("password", "duolaibei1");
//        param.add("ftpSavePath", "ftpFile/data");
//        param.add("ftpFileName", "666.png");
//        param.add("localFilePath", "D:/ftp/tp.png");
//        HttpUtils.getInstance().url("http://47.105.110.29:8080/hzxlma/uploadftp?").params(param).executeGet(new HttpUtils.HttpListener() {
//            @Override
//            public void onHttpSuccess(String string) {
//                super.onHttpSuccess(string);
//                LogDebug.d("--------ftp测试-------" + string);
//            }
//        });
//
////
////        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
////        param.add("data", "duolaibei1.cailianpay.cn");
////
////        HttpUtils.getInstance().url("http://47.105.110.29:8080/hzxlma/getJson?").params(param).executeGet(new HttpUtils.HttpListener() {
////            @Override
////            public void onHttpSuccess(String string) {
////                super.onHttpSuccess(string);
////                LogDebug.d("--------ftp测试-------" + string);
////            }
////        });
//
//    }
//
//    /**
//     * 获取富友的签名请求字符串
//     */
//    private void ftpFuyouReq() {
//
//        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
//        param.add("agencyType", "0");
//        param.add("insCd", "08M0026088");
//        param.add("traceNo", "1215121213131004");
//        param.add("jsapiPath", "http://www.duolaibei.com/plugins/payment/fuyouweixin/jspay/out_trade_no/");
//        param.add("mchntCd", "0003310F1750082");
//        param.add("subAppid", "wxd8978cc4068bc70c");
//        param.add("subscribeAppid", "wxd8978cc4068bc70c");
//        param.add("key", "7bb8deec0bd603e85ffe5732e65893c3");
//        HttpUtils.getInstance().url("http://47.105.110.29:8080/hzxlma/getfyReqParam?").params(param).executeGet(new HttpUtils.HttpListener() {
//            @Override
//            public void onHttpSuccess(String string) {
//                super.onHttpSuccess(string);
//                try {
//                    LogDebug.d("--------fy签名-------" + new JSONObject(string).optString("reqParam"));
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }

}
